#![feature(proc_macro_span, extend_one, core_intrinsics)]

use proc_macro::TokenStream;
use std::{str::FromStr, sync::Mutex};

mod generate;
mod parse;

static mut ALL_STRUCT: Vec<parse::Struct> = Vec::new();
static mut LOCK: Mutex<i32> = Mutex::new(0);

/// ...
/// 可选参数:[]
/// ...
#[proc_macro_attribute]
pub fn net_ser(args: TokenStream, input: TokenStream) -> TokenStream
{
    unsafe {
        let mut l = LOCK.lock().unwrap();
        println!("args = {:#?}", args);
        println!("input = {:#?}", input);
        let parse_result = parse::Struct::new(input.clone());
        let mut output = input;
        match parse_result
        {
            Ok(parse_struct) =>
            {
                println!("parse_struct = {:#?}", parse_struct);

                let generate_str = generate::generate(&parse_struct);
                println!("generate_str = {}", generate_str);

                let generate_result = TokenStream::from_str(generate_str.as_str());
                if let Ok(generate_token) = generate_result
                {
                    output.extend(generate_token);
                    ALL_STRUCT.push(parse_struct);
                }
                else
                {
                    println!("生成内容有错误,无法转换为TokenStream = {}", generate_str);
                }
            }
            Err(parse_error) =>
            {
                output.extend(parse_error);
            }
        }
        println!("output = {:#?}", output);
        *l += 1;
        output
    }
}
